package com.hsnn.medstgmini.settlement.controller;

import java.lang.reflect.Type;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.settlement.model.SetPayment;
import com.hsnn.medstgmini.settlement.model.form.Payment;
import com.hsnn.medstgmini.settlement.model.form.SetSettlementForm;
import com.hsnn.medstgmini.settlement.service.PaymentManager;
import com.hsnn.medstgmini.settlement.service.SettlementFirstManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 支付单
 * @author 蔡春龙
 * @date 2015年7月27日
 */
@Controller
@RequestMapping("/payment")
public class PaymentController {

	private static final Logger log = Logger.getLogger(PaymentController.class);

	@Autowired
	private PaymentManager paymentManager;

	@Autowired
	private SettlementFirstManager manager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 跳转支付单列表页面
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toPaymentList")
	public String toPaymentList() {
		log.info("enter toPaymentList method!");
		return "/settle/paymentList";
	}

	/**
	 * @category 查询支付单列表
	 * @date 2015年7月27日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getPaymentList")
	@ResponseBody
	public Pagination getPaymentList(Payment payment) {
		log.info("enter getPaymentList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.getPaymentList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	
	/**
	 * 
	 *@category 跳转支付单查看页面
	 *@author 邱磊
	 *@date 2015年8月9日 下午3:24:55
	 *@return
	 */
	@RequestMapping("/toSubmitPaymentList")
	public String toSubmitPaymentList() {
		log.info("enter toPaymentList method!");
		return "/settle/submitPaymentList";
	}

	/**
	 * 
	 *@category 获取支付单查看列表数据
	 *@author 邱磊
	 *@date 2015年8月9日 下午3:25:04
	 *@param payment
	 *@return
	 */
	@RequestMapping("/getSubmitPaymentList")
	@ResponseBody
	public Pagination getSubmitPaymentList(Payment payment) {
		log.info("enter getSubmitPaymentList method");
		Pagination page = new Pagination(request);
		try {
			page.setForm(payment);
			page = paymentManager.getSubmitPaymentList(page);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	
	
	

	/**
	 * 
	 * @category 跳向根据配送企业分组确认支付列表页面（采购中心,没有卫生局）
	 * @author 邱磊
	 * @date 2015年7月27日 下午6:31:12
	 * @return
	 */
	@RequestMapping("/toConfirmPayByCompHospList")
	public String toConfirmPayByCompHospList() {
		return "/settle/confirmPayByCompHospList";
	}

	/**
	 * 
	 * @category 跳向根据配送企业分组确认支付列表页面（采购中心,有卫生局）
	 * @author 邱磊
	 * @date 2015年7月27日 下午6:31:12
	 * @return
	 */
	@RequestMapping("/toConfirmPayByCompHealList")
	public String toConfirmPayByCompHealList() {
		return "/settle/confirmPayByCompHealList";
	}

	/**
	 * 
	 * @category 获取根据配送企业分组确认支付列表数据（采购中心,没有卫生局）
	 * @author 邱磊
	 * @date 2015年7月27日 下午7:06:12
	 * @return
	 */
	@RequestMapping("/getConfirmPayByCompHospDataList")
	@ResponseBody
	public Pagination getConfirmPayByCompHospDataList() {
		Pagination page = new Pagination(request);
		page.getConditions().put("paymentBillId",
				request.getParameter("paymentBillId"));// 支付单号
		page.getConditions().put("compCode", request.getParameter("compCode"));// 企业编码
		page.getConditions().put("compName", request.getParameter("compName"));// 企业名称
		return paymentManager.getConfirmPayByCompHospDataList(page);
	}

	/**
	 * 
	 * @category 获取根据配送企业分组确认支付列表数据（采购中心,有卫生局）
	 * @author 邱磊
	 * @date 2015年7月27日 下午7:06:12
	 * @return
	 */
	@RequestMapping("/getConfirmPayByCompHealDataList")
	@ResponseBody
	public Pagination getConfirmPayByCompHealDataList() {
		Pagination page = new Pagination(request);
		page.getConditions().put("paymentBillId",
				request.getParameter("paymentBillId"));// 支付单号
		page.getConditions().put("compCode", request.getParameter("compCode"));// 企业编码
		page.getConditions().put("compName", request.getParameter("compName"));// 企业名称
		return paymentManager.getConfirmPayByCompHealDataList(page);
	}

	/**
	 * 
	 * @category 确认支付操作（采购中心点击“确认支付” 按钮）
	 * @author 邱磊
	 * @date 2015年7月28日 上午11:56:32
	 * @return
	 */
	@RequestMapping("/confirmPay")
	@ResponseBody
	public Pagination confirmPay(String paymentBillId, String delCompId) {// 支付单ID
		Pagination page = new Pagination();
		page.getConditions().put("paymentBillId", paymentBillId);
		page.getConditions().put("delCompId", delCompId);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		page.getConditions().put("payUser", user.getId());
		try {
			return paymentManager.confirmPay(page);
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
			page.setMsg("支付失败！");
			return page;
		}
	}

	/**
	 * @category 保存支付单
	 * @date 2015年7月27日
	 * @param payment
	 * @return
	 */
	@RequestMapping("/savePayment")
	@ResponseBody
	public Pagination savePayment(SetPayment payment) {
		log.info("enter getPaymentList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.savePayment(payment, getUser());
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 跳转支付单添加明细页面
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toPaymentAddDetailList")
	public String toPaymentAddDetailList(Model model, String paymentBillId) {
		log.info("enter toPaymentAddDetailList method!");
		model.addAttribute("paymentBillId", paymentBillId);
		SetPayment payment = paymentManager.getPaymentById(paymentBillId);
		model.addAttribute("payment", payment);
		return "/settle/paymentAddDetailList";
	}

	/**
	 * @category 查询未加入支付单的结算单
	 * @date 2015年7月27日
	 * @param payment
	 * @return
	 */
	@RequestMapping("/getNotAddSettlementList")
	@ResponseBody
	public Pagination getNotAddSettlementList(Payment payment) {
		log.info("enter getPaymentList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.getNotAddSettlementList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 跳转支付单明细页面(详情)
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toPaymentDetailList")
	public String toPaymentDetailList(Model model, String paymentBillId) {
		log.info("enter toPaymentDetailList method!");
		model.addAttribute("paymentBillId", paymentBillId);
		SetPayment payment = paymentManager.getPaymentById(paymentBillId);
		model.addAttribute("payment", payment);
		return "/settle/paymentDetailList";
	}
	/**
	 * 
	 *@category 跳转到支付单查看详情页面
	 *@author 邱磊
	 *@date 2015年8月10日 下午8:32:58
	 *@param model
	 *@param paymentBillId
	 *@return
	 */
	@RequestMapping("/toSubmitPaymentDetailList")
	public String toSubmitPaymentDetailList(Model model, String paymentBillId) {
		log.info("enter toPaymentDetailList method!");
		model.addAttribute("paymentBillId", paymentBillId);
		SetPayment payment = paymentManager.getPaymentById(paymentBillId);
		model.addAttribute("payment", payment);
		return "/settle/submitPaymentDetailList";
	}

	/**
	 * @category 查询加入支付单的结算单(详情列表)
	 * @date 2015年7月27日
	 * @param payment
	 * @return
	 */
	@RequestMapping("/getPaymentDetailList")
	@ResponseBody
	public Pagination getPaymentDetailList(Payment payment) {
		log.info("enter getPaymentDetailList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.getPaymentDetailList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	@RequestMapping("/getSetmentFirstDetailList")
	@ResponseBody
	public Pagination getSetmentFirstDetailList(String settlementId){
		Pagination page = new Pagination(request);
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("hospName", request.getParameter("hospName"));
		try {
			paymentManager.getSetmenDetailList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	/**
	 * @category 添加支付明细
	 * @date 2015年7月27日
	 * @param payment
	 * @return
	 */
	@RequestMapping("/addPaymentDetail")
	@ResponseBody
	public Pagination addPaymentDetail(String array) {
		log.info("enter addPaymentDetail method");
		Pagination page = new Pagination(request);
		try {
			if (StringUtils.isNotBlank(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<Payment>>() {
				}.getType();
				List<Payment> list = gson.fromJson(array, type);
				paymentManager.addPaymentDetail(list, getUser());
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 删除支付单明细
	 * @date 2015年7月27日
	 * @param payment
	 * @return
	 */
	@RequestMapping("/deletePaymentDetail")
	@ResponseBody
	public Pagination deletePaymentDetail(String array) {
		log.info("enter deletePaymentDetail method");
		Pagination page = new Pagination(request);
		try {
			if (StringUtils.isNotBlank(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<Payment>>() {
				}.getType();
				List<Payment> list = gson.fromJson(array, type);
				paymentManager.deletePaymentDetail(list, getUser());
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 提交支付单
	 * @date 2015年7月27日
	 * @param payment
	 * @return
	 */
	@RequestMapping("/submitPayment")
	@ResponseBody
	public Pagination submitPayment(String array) {
		log.info("enter submitPayment method");
		Pagination page = new Pagination(request);
		try {
			if (StringUtils.isNotBlank(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<Payment>>() {
				}.getType();
				List<Payment> list = gson.fromJson(array, type);
				paymentManager.submitPayment(list, getUser());
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 跳转支付明细列表页面(支付单详情页面的详情链接)
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toSettlementDetailList")
	public String toSettlementDetailList(Model model, Payment payment) {
		log.info("enter toSettlementDetailList method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager
				.getSettlementInfoById(settlementId);
		model.addAttribute("settlement", settlement);
		model.addAttribute("settlementId2",request.getParameter("settlementId2"));
		return "/settle/paymentAddDetailListDetail";
	}
	////////////////////////////////////////////////////////
	/**
	 * @category 跳转支付查看，支付单支付明细
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toSettlementPayDetailList")
	public String toSettlementPayDetailList(Model model, Payment payment) {
		log.info("enter toSettlementDetailList method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager
				.getSettlementInfoById(settlementId);
		model.addAttribute("settlement", settlement);
		return "/settle/submitPaymentDetailListDetail";
	}
	/**
	 * @category 跳转支付查看，支付单支付明细
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toSetSettlementFirstDetailList")
	public String toSetSettlementFirstDetailList(Model model, Payment payment) {
		log.info("enter toSetSettlementFirstDetailList method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager
				.getSettlementInfoById(settlementId);
		model.addAttribute("info", settlement);
		return "/settle/submitSetmentDetailListDetail";
	}
	/**
	 * @category 查询支付明细列表
	 * @date 2015年7月27日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getSettlementDetailList")
	@ResponseBody
	public Pagination getSettlementDetailList(Payment payment) {
		log.info("enter getSettlementDetailList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.getSettlementDetailList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category 获取
	 *@author 邱磊
	 *@date 2015年8月10日 下午9:52:38
	 *@param payment
	 *@return
	 */
	@RequestMapping("/getPaymentPayDetailList")
	@ResponseBody
	public Pagination getPaymentPayDetailList(Payment payment) {
		log.info("enter getPaymentPayDetailList method");
		Pagination page = new Pagination(request);
		try {
			page.setForm(payment);
			paymentManager.getPaymentPayDetailList(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 删除支付单(未提交的)
	 * @date 2015年7月28日
	 * @param payment
	 * @return
	 */
	@RequestMapping("/deletePayment")
	@ResponseBody
	public Pagination deletePayment(String array) {
		log.info("enter deletePayment method");
		Pagination page = new Pagination(request);
		try {
			if (StringUtils.isNotBlank(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<Payment>>() {
				}.getType();
				List<Payment> list = gson.fromJson(array, type);
				paymentManager.deletePayment(list, getUser());
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 跳转支付明细列表页面(添加支付明细页面的详情链接)
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toAddSettlementDetailList")
	public String toAddSettlementDetailList(Model model, Payment payment) {
		log.info("enter toAddSettlementDetailList method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager
				.getSettlementInfoById(settlementId);
		model.addAttribute("settlement", settlement);
		return "/settle/addSettlementDetailList";
	}

	/**
	 * @category 查询支付明细列表
	 * @date 2015年7月27日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getAddSettlementDetailList")
	@ResponseBody
	public Pagination getAddSettlementDetailList(Payment payment) {
		log.info("enter getAddSettlementDetailList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.getAddSettlementDetailList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	//////////////////////////////////////////////////////////////////////////////
	/**
	 * @category 跳转支付查看，支付单支付明细
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toSetmentDetailListDetail")
	public String toSetmentDetailListDetail(Model model, Payment payment) {
		log.info("enter toSetmentDetailListDetail method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager
				.getSettlementInfoById(settlementId);
		model.addAttribute("info", settlement);
		return "/settle/setmentDetailListDetail";
	}
	
	@RequestMapping("/getSetSettlementFirstDetailList")
	@ResponseBody
	public Pagination getSetSettlementFirstDetailList(String settlementId){
		Pagination page = new Pagination(request);
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("hospName", request.getParameter("hospName"));
		try {
			paymentManager.getSetmenDetailList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 跳转支付明细列表页面(支付单详情页面的详情链接)
	 * @date 2015年7月27日
	 * @return
	 */
	@RequestMapping("/toSetmentAddDetailListDetail")
	public String toSetmentAddDetailListDetail(Model model, Payment payment) {
		log.info("enter toSetmentAddDetailListDetail method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager
				.getSettlementInfoById(settlementId);
		model.addAttribute("settlement", settlement);
		model.addAttribute("settlementId2",request.getParameter("settlementId2"));
		return "/settle/setmentAddDetailListDetail";
	}
	/**
	 * @category 查询支付明细列表
	 * @date 2015年7月27日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getSetmentDetailList")
	@ResponseBody
	public Pagination getSetmentDetailList(Payment payment) {
		log.info("enter getSetmentDetailList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.getSettlementDetailList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	//////////////////////////////////////////////////////////////////////////////
	/**
	* @category 跳转支付查看，支付单支付明细
	* @date 2015年7月27日
	* @return
	*/
	@RequestMapping("/toSetmentNewDetailListDetail")
	public String toSetmentNewDetailListDetail(Model model, Payment payment) {
		log.info("enter toSetmentNewDetailListDetail method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager
		.getSettlementInfoById(settlementId);
		model.addAttribute("info", settlement);
		return "/settle/setmentNewDetailListDetail";
	}
	
	@RequestMapping("/getSetSettlementNewFirstDetailList")
	@ResponseBody
	public Pagination getSetSettlementNewFirstDetailList(String settlementId){
		Pagination page = new Pagination(request);
		page.getConditions().put("settlementId", settlementId);
		page.getConditions().put("hospName", request.getParameter("hospName"));
		try {
			paymentManager.getSetmenDetailList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	* @category 跳转支付明细列表页面(支付单详情页面的详情链接)
	* @date 2015年7月27日
	* @return
	*/
	@RequestMapping("/toSetmentAddNewDetailListDetail")
	public String toSetmentAddNewDetailListDetail(Model model, Payment payment) {
		log.info("enter toSetmentAddNewDetailListDetail method!");
		model.addAttribute("payment", payment);
		String settlementId = payment.getSettlementId();
		SetSettlementForm settlement = manager.getSettlementInfoById(settlementId);
		model.addAttribute("settlement", settlement);
		model.addAttribute("settlementId2",request.getParameter("settlementId2"));
		return "/settle/setmentAddNewDetailListDetail";
	}
	/**
	* @category 查询支付明细列表
	* @date 2015年7月27日
	* @param address
	* @return
	*/
	@RequestMapping("/getSetmentNewDetailList")
	@ResponseBody
	public Pagination getSetmentNewDetailList(Payment payment) {
		log.info("enter getSetmentNewDetailList method");
		Pagination page = new Pagination(request);
		try {
			paymentManager.getSettlementDetailList(payment, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	
	
}	

